define(['mui', 'ajax'], function() {
	mui.init();
	var ws = null,
		user = null;
	var openlogin = "0";
	var transtime = null;
	var news = null,
		analysis = null,
		h = null;
	var main = null;

	mui.plusReady(function() {
		main = plus.webview.currentWebview().opener();

		ws = plus.webview.currentWebview();
		transtime = getNowFormatDate();
		h = plus.webview.getWebviewById(plus.runtime.appid); //获取应用入口页面的方法
		news = plus.webview.getWebviewById("report.html")
		analysis = plus.webview.getWebviewById("pages/analysis.html")
		//判断显示文字
		console.log('meun')
		user = judgeLogin();
		if(user) {
			getuser();
		} else {
			document.getElementById("signout").classList.add('mui-hidden');
			document.getElementById("signoutul").classList.add('mui-hidden');
			
		}
		document.getElementById('userinfo').addEventListener('tap', function() {
			closeMenu();
			user = judgeLogin();
			console.log('%o',user)
			if(user) {
				
				openpage("/src/pages/user_info.html")
			} else {
				plus.nativeUI.toast("请登录");
				openpage('/src/pages/login_standard.html');
			}
		});
		mui("#userlist").on('tap', 'li', function() {
			closeMenu();
			if(unsafe_tap()) return; //防御编码
			//点击事件
			user = judgeLogin();
			
			var page = this.getAttribute("id");
			console.log('%o',page)
			if(page == "signout") {
				if(user) {
					loginout(user);
				}
			} else if(page == "/src/pages/about.html" || page == "/src/pages/about_company.html") {
				openpage(page);
			} else {
				if(user) {
					openpage(page);
				} else {
					plus.nativeUI.toast("请登录");
					openpage('/src/pages/login_standard.html');
				}
			}

		});
		var first = null;
		mui.back = function() {
			//首次按键，提示‘再按一次退出应用’
			if(!first) {
				first = new Date().getTime();
				mui.toast('再按一次退出应用');
				setTimeout(function() {
					first = null;
				}, 1000);
			} else {
				if(new Date().getTime() - first < 1000) {
					plus.runtime.quit();
				}
			}
		};

		//修改密码之后触发此事件，退出登录，重新登录
		document.addEventListener('changepwdLogintout', function() {
			user = judgeLogin();
			loginout(user);
		})

	});
	//用户退出
	function loginout(user) {
		plus.storage.removeItem('memberdata');
		//先更新news
		h.reload(false);
		document.getElementById("touxiangword").innerHTML = '请登录';
		document.getElementById("companyName").innerHTML = '';
		document.getElementById("agreementTime").innerHTML = '';
		document.getElementById("trueimg").src = "../library/images/img/avatar.jpg"
		document.getElementById("signout").classList.add('mui-hidden');
		document.getElementById("signoutul").classList.add('mui-hidden');
		mui.fire(h, 'changeHeadImgIndex', {

		});
		plus.nativeUI.toast("退出登录成功");
		mui.post(ajax_loginoutURL, {
			transtime: transtime,
			token: user.token
		}, function(data) {
			//h.reload(false);
		}, 'json');
		
	}
	//登陆成功之后，更新个人信息
	function getuser() {
		user = judgeLogin();
		if(user) {
			document.getElementById("signout").classList.remove('mui-hidden');
			document.getElementById("signoutul").classList.remove('mui-hidden');
			var token = user.token;
			//				 	if (user.alias){
			//				 		document.getElementById("touxiangword").innerHTML= user.id + '<span>'+ user.alias+ '</span>';
			//				 	}else {
			//				 		document.getElementById("touxiangword").innerHTML= user.id + '<span></span>';
			//				 	}

			document.getElementById("touxiangword").innerHTML = user.name == undefined ? '' : user.name + '<span></span>';
			if(user.company) {
				document.getElementById("companyName").innerHTML = user.company;
			} else {
				document.getElementById("companyName").innerHTML = '请完善个人信息';
			}
			if(user.starttime && user.endtime) {
				document.getElementById("agreementTime").innerHTML = '使用时间:' + user.starttime + '到' + user.endtime
			} else {
				document.getElementById("agreementTime").innerHTML = '';
			}
			if(user.headimg) {
				document.getElementById("trueimg").src = main_host + user.headimg
			}
		}

		token = null;
		user = null;
	}

	function closeMenu() {
		mui.fire(main, "menu:swipeleft");
	}
	window.addEventListener("swipeleft",closeMenu);  
			mui.menu = closeMenu;
			  //登陆后出发此事件获取个人信息并加载
			   window.addEventListener('getUserInfoFromServer',function(event) {
			   		user = judgeLogin();
			   		console.log('user:',JSON.stringify(user))
			   		var token = user.token;
			   		mui.post(ajax_menberInfoURL,{
								transtime:transtime,
								token:token
							},function(data){
								console.log('userinfo:',data);
								data = JSON.parse(data);
								if  (data.result == 1){
	//								mui.toast('更新个人资料成功');
									user.token = token;
									user.id = data.UserID;
									user.name = data.UserName;
									user.headimg = data.Avatar;
									user.company = data.MemberName;
									user.address = data.Address;
									user.alias = data.MemberAlias;
									user.starttime = data.StartTime;
									user.endtime = data.EndTime;
									user.grounpname = data.GroupName;
									user.amout = data.amount;
									var memberdata=JSON.stringify(user);
									plus.storage.setItem('memberdata',memberdata);
									
									//根据个人信息展示
									getuser();
									mui.fire(h,'changeHeadImgIndex',{
									
									});
								}
							},'json'
						);
			   });
})